package com.example.yls.demoa;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private static final int MSG_PROGRESS_CHAGED = 1001;
private Button btnStart;
private Button btnpause;
private Button btnStop;
private MediaPlayer mediaPlayer= new MediaPlayer();
private SeekBar seekBar;
private TextView txtCur;
private TextView txtTotal;
private boolean isPause = false;
private Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = findViewById(R.id.btn_start);
        btnpause = findViewById(R.id.btn_pause);
        btnStop = findViewById(R.id.btn_stop);
        seekBar = findViewById(R.id.seekbar);
        txtCur = findViewById(R.id.curTime);
        txtTotal = findViewById(R.id.totalTime);


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
           @Override
           public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

           }

           @Override
           public void onStartTrackingTouch(SeekBar seekBar) {

           }

           @Override
           public void onStopTrackingTouch(SeekBar seekBar) {
               if(mediaPlayer!= null){
                   int progress = seekBar.getProgress();
                   int total = mediaPlayer.getDuration();
                   int position = total*progress/100;
                   mediaPlayer.seekTo(position);
               }

           }
       });
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if(message.what == MSG_PROGRESS_CHAGED){
                    int position = message.arg1;
                    int duration = message.arg2;
                    txtCur.setText(getTimeStr(position));
                    txtTotal.setText(getTimeStr(duration));
                }


                return false;
            }
        });

       new Thread(new Runnable() {
           @Override
           public void run() {
               while(true){
                   if(mediaPlayer != null && mediaPlayer.isPlaying()){
                       int duration = mediaPlayer.getDuration();
                       int position = mediaPlayer.getCurrentPosition();
                       int progress = position * 100 / duration ;
                       seekBar.setProgress(progress);


                       Message message = new Message();
                       message.what = MSG_PROGRESS_CHAGED;
                       message.arg1 = position;
                       message.arg2 = duration;
                       handler.sendMessage(message);
                   }
                   try{
                       Thread.sleep(500);
                   }catch (InterruptedException e ){
                       e.printStackTrace();
                   }
               }
           }
       }).start();

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isPause){
                    mediaPlayer.start();
                    isPause = false;
                }else {
                    // 第一次或者停止后启动
                    try {
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource("/Nox_shared/Other/shangfeng.aac");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                        }
                    });
                    mediaPlayer.prepareAsync();
                }
            }
        });

        btnpause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayer != null && mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                    isPause = true;
                }
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayer != null && mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                }
            }
        });

    }
    private String getTimeStr(int time) {
        int second = time / 1000;
        int min = second / 60;
        second = second % 60;

        StringBuffer buffer = new StringBuffer();
        if(min < 10){
            buffer.append("0");
        }

        buffer.append(min);
        buffer.append(":");

        if(second < 10){
            buffer.append("0");
        }
        buffer.append(second);

        return  buffer.toString();
    }
}
